#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(){

  FILE* file =  fopen("./my.txt","w");
  //向文件里写入数据
  int count = 0;
  char* str = "i am zgy!\n";
  while(count<5){
    size_t finish = fwrite(str,1,strlen(str),file);
    printf("i had write %d alpha  into file!\n",finish);
    sleep(1);
    ++count;
  }
  fclose(file);

  //从文件中读取数据
  //要将打开文件的属性换位读
  file = fopen("./my.txt","r");
  while(count>=0){
    char buf[128] = "0";
    size_t finish = fread(buf,1,strlen(str),file);
    buf[finish] = 0;
    printf("i read %d alpha from file!\n",finish);
    printf("%s\n",buf);
    sleep(1);
    --count;
  }
  return 0;
}
